package gui.utils;

import gui.cen.Center;
import gui.win.tree.FullT;
import java.awt.*;
import java.text.DecimalFormat;
import javax.swing.*;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class SliderWrap extends JPanel{
    private Slider sld;
    private JLabel lab;
    private JTextField val;

    public SliderWrap(int which, int maxVal, int minVal, int initialVal, String lab, int width){
        super(new FlowLayout());
        this.setOpaque(false);
        this.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
        
        if(!lab.equals("")){
            if(lab.indexOf("|")!=-1){
                sld = new Slider(which, maxVal, minVal, initialVal, this, width);
                this.add(sld);
                this.lab = new JLabel(lab);
                this.lab.setFont(Borders.getFont(Borders.GUIFONT));
                this.lab.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
                this.add(this.lab);
            }
            else{
                this.lab = new JLabel(lab);
                this.lab.setFont(Borders.getFont(Borders.GUIFONT));
                this.lab.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
                this.add(this.lab);
                sld = new Slider(which, maxVal, minVal, initialVal, this, width);
                this.add(sld);
            }
        }
        else{
            sld = new Slider(which, maxVal, minVal, initialVal, this, width);
            this.add(sld);
        }
    }

    public SliderWrap(int which, int maxVal, int minVal, int initialVal, String lab, int width, int txtCols){
        super(new BorderLayout());
        this.setOpaque(false);
        this.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
        if(!lab.equals("")){
            this.lab = new JLabel(lab);
            this.lab.setFont(Borders.getFont(Borders.GUIFONT));
            this.lab.setBorder(BorderFactory.createEmptyBorder(0,0,0,8));
            this.add(this.lab, BorderLayout.WEST);
        }
        sld = new Slider(which, maxVal, minVal, initialVal, width, this);
        this.add(sld, BorderLayout.CENTER);
        val = new JTextField(initialVal+"", txtCols);
        val.setOpaque(false);
        val.setBorder(null);
        val.setFont(Borders.getFont(Borders.GUIFONT));
        val.setEditable(false);
        this.add(val, BorderLayout.EAST);
    }

    public SliderWrap(int which, int maxVal, int minVal, int initialVal, String lab, int width, int txtCols, FullT FullTreeL0){
        super(new BorderLayout());
        this.setOpaque(false);
        this.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
        if(!lab.equals("")){
            this.lab = new JLabel(lab);
            this.lab.setFont(Borders.getFont(Borders.GUIFONT));
            this.lab.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
            this.add(this.lab, BorderLayout.WEST);
        }

        sld = new Slider(which, maxVal, minVal, initialVal, width, this, FullTreeL0);
        this.add(sld, BorderLayout.CENTER);
        val = new JTextField(initialVal+"", txtCols);
        val.setOpaque(false);
        val.setBorder(null);
        val.setFont(Borders.getFont(Borders.GUIFONT));
        val.setEditable(false);
        this.add(val, BorderLayout.EAST);
    }

    public SliderWrap(int which, int maxVal, int minVal, int initialVal, String lab, int width, Center center){
        super(new FlowLayout());
        this.setOpaque(false);
        this.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
        if(!lab.equals("")){
            this.lab = new JLabel(lab);
            this.lab.setFont(Borders.getFont(Borders.GUIFONT));
            this.lab.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
            this.add(this.lab);
        }

        sld = new Slider(which, maxVal, minVal, initialVal, width, this, center);
        this.add(sld);
    }

    private double div;
    public SliderWrap(int which, int maxVal, int minVal, int initialVal, String lab, int width, int txtCols, Center center, boolean willtakeDouble, double div, Color fontColor, DecimalFormat df){
        super(new BorderLayout());
        this.setOpaque(false);
        this.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
        if(!lab.equals("")){
            this.lab = new JLabel(lab);
            this.lab.setFont(Borders.getFont(Borders.GUIFONT));
            this.lab.setForeground(fontColor);
            this.lab.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
            this.add(this.lab, BorderLayout.WEST);
        }
        
        sld = new Slider(which, maxVal, minVal, initialVal, width, this, center);
        this.add(sld, BorderLayout.CENTER);
        val = new JTextField(initialVal+"", txtCols);
        val.setForeground(fontColor);
        val.setBorder(null);
        val.setOpaque(false);
        val.setFont(Borders.getFont(Borders.GUIFONT));
        val.setEditable(false);
        this.div = div;
        this.df = df;
        if(willtakeDouble){setLabelSpecial(initialVal);}
        this.add(val, BorderLayout.EAST);
    }

    public void setLabel(int val){
        if(val>=0){this.val.setText(val+"");}
        else{this.val.setText(val+"");}
    }

    private DecimalFormat df;
    public void setLabelSpecial(int val){
        if(val>=0){this.val.setText(df.format((double)val/div));}
        else{this.val.setText(val+"");}
    }
}